package com.echoesnet.echoesshortvideo.ViewModels;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;

import com.echoesnet.echoesshortvideo.Api.Subscriber.SilenceSubscriber;
import com.echoesnet.echoesshortvideo.Beans.UserBean;
import com.echoesnet.echoesshortvideo.Repositories.LoginRepository;
import com.echoesnet.echoesshortvideo.Repositories.RegisterRepository;
import com.echoesnet.echoesshortvideo.Utils.SharePreUtils;

import org.json.JSONException;
import org.json.JSONObject;

import javax.xml.transform.Result;

import io.reactivex.Observer;

/**
 * Created by Administrator on 2018/4/24.
 *
 * @author ling
 */
public class StepRegisterViewModel
{

    private RegisterRepository loginRepository;
    public LiveData<String> getLiveData()
    {
        return loginLive;
    }

    private MutableLiveData<String> loginLive=new MutableLiveData<>();

    public StepRegisterViewModel()
    {
        loginRepository=new RegisterRepository();
    }

    public void register(String phone,String password)
    {
        if (loginRepository==null)
            throw new NullPointerException("LoginRepository不可为空");
        loginRepository.register(phone,password,new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject jb = new JSONObject(response);
                    SharePreUtils.setUId(jb.getString("userId"));
                    SharePreUtils.setToken(jb.getString("token"));

                    loginLive.setValue("registerOk");
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }


    public void sendVerificationCode(String phone, String  channel)
    {
        if (loginRepository==null)
            throw new NullPointerException("LoginRepository不可为空");
        loginRepository.sendVerificationCode(phone,channel,new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);

                try
                {
                    JSONObject jb = new JSONObject(response);
                    loginLive.setValue(jb.getString("userId"));
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }


            }
        });
    }
    public void sendVoiceCode(String phone, String  channel)
    {
        if (loginRepository==null)
            throw new NullPointerException("LoginRepository不可为空");
        loginRepository.sendVoiceCode(phone,channel,new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);

                try
                {
                    JSONObject jb = new JSONObject(response);
                    loginLive.setValue(jb.getString("userId"));
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }


            }
        });
    }

    public void checkVerifyCode(String phone,String verifyCode,String channel)
    {
        if (loginRepository==null)
            throw new NullPointerException("LoginRepository不可为空");
        loginRepository.checkVerifyCode(phone,verifyCode,channel,new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                loginLive.setValue("checkCodeOk");
            }
        });
    }


    public void makeUserInfo(UserBean user)
    {
        if (loginRepository==null)
            throw new NullPointerException("LoginRepository不可为空");

        loginRepository.makeUserInfo(user,new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                loginLive.setValue("makeInfoOk");
            }
        });
    }
    public void bindPhone(String phone,String password)
    {
        if (loginRepository==null)
            throw new NullPointerException("LoginRepository不可为空");

        loginRepository.bindPhone(phone,password,new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                loginLive.setValue("bindPhoneOk");
            }
        });
    }
}
